package com.SouthernWall_404.Age_Capitalism.common.Network.C2S;

import com.SouthernWall_404.Age_Capitalism.common.Network.ClientHandler.MerchantClientHandler;
import com.SouthernWall_404.Age_Capitalism.common.World.Entity.StoreBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.trading.MerchantOffers;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;

import java.util.function.Supplier;

public class AddingMerchantPacket {

    private final MerchantOffers offers;
    public BlockPos blockPos;

    public AddingMerchantPacket(MerchantOffers merchantOffers32457_,BlockPos blockPos) {
        this.offers = merchantOffers32457_;
        this.blockPos=blockPos;
    }

    public AddingMerchantPacket(FriendlyByteBuf p_178985_) {
        this.offers = MerchantOffers.createFromStream(p_178985_);
        this.blockPos=p_178985_.readBlockPos();
    }

    public void encode(FriendlyByteBuf p_132470_) {
        this.offers.writeToStream(p_132470_);
        p_132470_.writeBlockPos(this.blockPos);
    }

    public void handle(Supplier<NetworkEvent.Context> supplier)
    {
        var content=supplier.get();
        content.enqueueWork(() -> {

            Level level=content.getSender().level();
            BlockEntity blockEntity=level.getBlockEntity(this.blockPos);
            if(blockEntity instanceof StoreBlockEntity storeBlockEntity)
            {
                storeBlockEntity.overrideOffers(this.offers);
            }

        });


        content.setPacketHandled(true);
    }

    public MerchantOffers getOffers() {
        return this.offers;
    }
}
